package com.tb.controller;

import com.tb.dubbo.IGoodsService;
import com.tb.dubbo.ISolrService;
import com.tb.pojo.Goods;
import com.tb.pojo.Img;
import com.tb.pojo.*;
import com.tb.utils.Msg;
import com.tb.utils.ReturnList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/Goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISolrService iSolrService;

    /**
     * @api {delete} /Goods/delete 后台管理商品下架  杜飞虎
     * @apiDescription 下架商品及其详情信息
     * @apiGroup Goods API
     *
     * @apiParam {String}  goodsId 商品id
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "goodsId":56453213
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *     {
     *        "code": 0,
     *         "msg": "",
     *         "data":"SUCCESS"
     *      }
     *
     *
     * @apiErrorExample {json} 错误返回值:
     *       {
     *           "code": 1,
     *           "msg": "failed",
     *           "data": null
     *       }
     * @apiErrorExample {json} 错误返回值:
     *            {
     *                 "code": 999,
     *                 "msg": "登陆超时",
     *                 "data": null
     *             }
     */
    @DeleteMapping ("/delete")
    public Msg deleteGoods(@RequestParam(value = "goodsId",defaultValue = "0") String goodsId, HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute( "AdminUser" );
            user.getUserId();
            if (goodsId == null) {
                return Msg.error( 1, "failed" );
            }
            /*List list = new ArrayList();
            for (int id : goodsId) {
                list.add( id );
            }*/
            int num = goodsService.deleteGoods( goodsId );
            if (num != 0) {
                iSolrService.addSolr();
                return Msg.success( "SUCCESS" );
            } else {
                String msg = "failed";
                Integer code = 1;
                return Msg.error( 1, msg );
            }
        }catch (Exception e){
            return Msg.error( 999,"登录超时！" );
        }
    }

    /**
     * @api {put} /Goods/update 修改商品    杜飞虎
     * @apiDescription 修改商品及其详情信息
     * @apiGroup Goods API
     *
     * @apiParam {String}  goodsId 商品id
     * @apiParam {String}  goodsName 商品名称
     * @apiParam {Float}  goodsPrice 商品价格
     * @apiParam {Integer}  goodsNum 商品数量
     * @apiParam {Integer}  goodsKind 商品种类id
     * @apiParam {Float}  goodsWeight 商品重量
     * @apiParam {Float}  goodsDiscount 商品折扣
     *
     * @apiParam {Object}  img 图片对象
     * @apiParam {Integer}  id 图片编号
     * @apiParam {String}  imgUrl 图片链接
     * @apiParam {String}  goodsId 商品id
     *
     * @apiParam {Object}  natures 商品详情对象
     * @apiParam {Integer}  id 商品详情编号
     * @apiParam {String}  nature 商品详情名
     * @apiParam {String}  goodsId 商品编号
     *
     * @apiParam {Integer}  naturev 商品详情信息编号
     * @apiParam {Object}  naturev 商品详情信息对象
     * @apiParam {Integer}  natureId 商品详情编号
     * @apiParam {Map}  map 商品详情信息值
     *
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     * 		"goodsId": "",
     * 		"goodsName": "苹果66s",
     * 		"goodsPrice": 999,
     * 		"goodsNum": 45,
     * 		"goodsKind": 1,
     * 		"goodsWeight": 145,
     * 		"goodsDiscount": null,
     *
     * 	    "img":[{
     * 		    "id":0,
     * 		    "goodsId":"",
     * 			"imgUrl":"ssssssfdgfgfdg"
     * 		},
     * 		{
     * 		    "id":0,
     * 		    "goodsId":"",
     * 			"imgUrl":"dddffffffffff"
     * 		}
     * 		],
     *
     * 		"natures": [{
     * 		    "id":1,
     * 		    "nature":"使用信息",
     * 		    "goodsId":"111",
     * 		    "naturev":[{
     * 		        "id":0,
     * 		        "natureId":0,
     * 		        "map": {
     * 		           "成色":"9成新"
     * 		       }
     * 		    },
     * 		    {
     * 		        "id":0,
     * 		        "natureId":0,
     * 		         "map": {
     * 		          "购买渠道":"国行"
     * 		       }
     * 		    }
     *
     * 		        ]
     * 		},
     *
     *         {
     * 		    "id":1,
     * 		    "nature":"型号信息",
     * 		    "goodsId":"111",
     * 		    "naturev":[{
     * 		        "id":0,
     * 		        "natureId":1,
     * 		       "map": {
     * 		         "品牌":"苹果"
     * 		       }
     * 		    },
     * 		    {
     * 		        "id":0,
     * 		        "natureId":1,
     * 		       "map": {
     * 		           "机型":"5s"
     * 		       }
     * 		    },
     * 		    {
     * 		        "id":0,
     * 		        "natureId":1,
     * 		        "map": {
     * 		           "颜色":"金色"
     * 		       }
     * 		    }
     *
     * 		        ]
     * 		},{
     * 		    "id":1,
     * 		    "nature":"参数信息",
     * 		    "goodsId":"111",
     * 		    "naturev":[{
     * 		        "id":0,
     * 		        "natureId":2,
     * 		       "map": {
     * 		           "内存":"16G"
     * 		       }
     * 		    },
     * 		    {
     * 		        "id":0,
     * 		        "natureId":2,
     * 		        "map": {
     * 		           "运行内存":"4G"
     * 		       }
     * 		    },
     * 		    {
     * 		        "id":0,
     * 		        "natureId":2,
     * 		        "map": {
     * 		           "cpu":"双核"
     * 		       }
     * 		    }
     *
     * 		        ]
     * 		}
     * ]
     * }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "",
     *     "data":"SUCCESS"
     *     }
     *
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "failed",
     *     "data": null
     * }
     * @apiErrorExample {json} 错误返回值:
     *                  {
     *                       "code": 999,
     *                      "msg": "登陆超时",
     *                       "data": null
     *                   }
     *
     * @apiSampleRequest /Goods/update
     */
    @PutMapping("/update")
    public Msg updateGoods(@RequestBody Goods2 goods2,HttpServletRequest request){
        try {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute( "AdminUser" );
            user.getUserId();

            boolean b = goodsService.updateGoods( goods2 );
            if (b == true) {
                return Msg.success( "SUCCESS" );
            }
            return Msg.error( 1, "failed" );
        }catch (Exception e){
            return Msg.error( 999,"登录超时！" );
        }
    }



    @PostMapping("/test")
    public Msg test(@RequestBody Goods2 goods2){
        System.out.println( goods2 );
        return Msg.success( goods2 );
    }


    /**
     * @api {get} /Goods/selectDtails 根据商品ID查询商品详情 宁凯悦
     * @apiDescription 根据商品ID查询商品详情
     * @apiGroup Goods API
     *
     * @apiParam {String} [goodsId=0] 商品ID
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "goodsId": 1
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码:<br/> <span style="color:#1AA034">0:正常 </span>
     *                    <br/> <span style="color:red">1:未找到商品</span>
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data 返回数据结果集
     * @apiSuccess  (data) {int} total 数据总条数
     * @apiSuccess  (data) {list} list 返回数据结果集
     * @apiSuccess  (list) {String} goodsId 商品id
     * @apiSuccess  (list) {String} goodsName 商品名称
     * @apiSuccess  (list) {float} goodsPrice 商品单价
     * @apiSuccess  (list) {int} goodsNum 商品数量
     * @apiSuccess  (list) {float} goodsWeight 商品重量
     * @apiSuccess  (list) {int} goodsSold 商品销量
     * @apiSuccess  (list) {String} goodsDiscount 商品折扣
     * @apiSuccess  (list) {ShopParam} listShopParam 商品颜色尺寸等
     * @apiSuccess  (list) {String} imgUrl 商品图片链接
     *
     * @apiSuccessExample {json} 正确返回值:
     *{
     *   "code": 0,
     *   "msg": "",
     *   "data": {
     *     "total": 1,
     *     "list": [
     *       {
     *         "goodsId": "154772714432427",
     *         "goodsName": "Apple 苹果 iPhone X 手机 银色 官方标配 64GB",
     *         "goodsPrice": 5000,
     *         "goodsNum": 1000,
     *         "goodsWeight": 200,
     *         "goodsSold": 1000,
     *         "goodsComments": 1,
     *         "goodsDiscount": 0.8,
     *         "orderField": "goods_id",
     *         "orderType": "desc"
     *       },
     *       [
     *         {
     *           "id": 27,
     *           "goodsId": "154772714432427",
     *           "imgUrl": "https://img14.360buyimg.com/n0/jfs/t11554/205/724805266/167107/a36460a5/59f80befNdf1814c5.jpg"
     *         },
     *         {
     *           "id": 31,
     *           "goodsId": "154772714432427",
     *           "imgUrl": "//img14.360buyimg.com//n0/jfs/t1/708/33/12050/20042/5bd072f9E6702c378/3d2b5d137aea371a.jpg"
     *         },
     *         {
     *           "id": 40,
     *           "goodsId": "154772714432427",
     *           "imgUrl": "https://img14.360buyimg.com/n0/jfs/t10039/191/2471035098/140437/8ff6cac7/59f80befN42c3b8d6.jpg"
     *         }
     *       ],
     *       [
     *         {
     *           "goodsTag": "尺寸",
     *           "goodsInfo": [
     *             "5.0",
     *             "4.3",
     *             "5.6",
     *             "6.5"
     *           ]
     *         },
     *         {
     *           "goodsTag": "颜色",
     *           "goodsInfo": [
     *             "黑色",
     *             "红色",
     *             "白色"
     *           ]
     *         },
     *         {
     *           "goodsTag": "容量",
     *           "goodsInfo": [
     *             "64G",
     *             "128G"
     *           ]
     *         }
     *       ]
     *     ]
     *   }
     * }
     * @apiErrorExample {json} 错误返回值:
     * {
     * "code": 1,
     * "msg": "未找到商品",
     * "data": null
     * }
     * @apiSampleRequest /Goods/selectDtails
     */
    @GetMapping("/selectDtails")
    public Msg selectGoods(@RequestParam(value = "goodsId", defaultValue = "0") String goodsId) {
        Map<String, String> map = new HashMap<>();
        map.put("gid", goodsId);
        Goods goods = goodsService.goodsDetails(map);
        if (goods == null) {
            return Msg.error(1, "未找到商品");
        }

        List<ShopParam> lparam = goodsService.getGoodsParam(map);
        Map<String, List<ShopParam>> map2 = lparam.stream().collect(Collectors.groupingBy(ShopParam::getType, LinkedHashMap::new,
                Collectors.mapping(Function.identity(), Collectors.toList())));
        List<Object> l = new ArrayList();
        Set<Map.Entry<String, List<ShopParam>>> set = map2.entrySet();
        for (Map.Entry<String, List<ShopParam>> entry : set) {
            String key = entry.getKey();
            List<ShopParam> value = entry.getValue();
            List<String> l2 = new ArrayList();
            Map m = new LinkedHashMap();
            m.put("goodsTag", key);
            for (ShopParam a : value) {
                String text = a.getText();
                l2.add(text);
            }
            m.put("goodsInfo", l2);
            l.add(m);
        }

        List<Img> imgs = goodsService.selectPic(map);

        List list = new ArrayList();
        if (goods != null) {
            list.add(goods);
            list.add(imgs);
            list.add(l);
            return Msg.success(ReturnList.get(1L, list));
        } else {
            return Msg.error(1, "未找到商品");
        }
    }

}
